package org.loofer.ext.utils;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Environment;
import android.os.StatFs;
import com.sina.weibo.sdk.component.ShareRequestParam;
import java.io.File;

public final class StorageUtils {
    private static final int STATE_MOUNTED = 0;
    private static final int STATE_MOUNTED_READ_ONLY = 1;
    private static final int STATE_OTHERS = 2;
    private static final int STATE_UNKNOWN = -1;
    private static final Object sCacheDirLock = new Object();
    private static int sMonitoredExternalState = -1;
    private static final Singleton<BroadcastReceiver, Void> sReceiver = new Singleton<BroadcastReceiver, Void>() {
        protected BroadcastReceiver create(Void param) {
            return new BroadcastReceiver() {
                public void onReceive(Context context, Intent intent) {
                    StorageUtils.onStorageStateChanged();
                }
            };
        }
    };
    private static volatile boolean sReceiverRegistered = false;

    static class InnerEnvironment {
        private static final String EXTEND_SUFFIX = "-ext";
        private static final File EXTERNAL_STORAGE_ANDROID_DATA_DIRECTORY = new File(new File(Environment.getExternalStorageDirectory(), "Android"), ShareRequestParam.RESP_UPLOAD_PIC_PARAM_DATA);
        private static final String TAG = "InnerEnvironment";

        InnerEnvironment() {
        }

        public static File getExternalStorageAndroidDataDir() {
            return EXTERNAL_STORAGE_ANDROID_DATA_DIRECTORY;
        }

        public static File getExternalStorageAppCacheDirectory(String packageName) {
            return new File(new File(EXTERNAL_STORAGE_ANDROID_DATA_DIRECTORY, packageName), "cache");
        }

        public static File getExternalStorageAppFilesDirectory(String packageName) {
            return new File(new File(EXTERNAL_STORAGE_ANDROID_DATA_DIRECTORY, packageName), "files");
        }

        /* JADX WARNING: inconsistent code. */
        /* Code decompiled incorrectly, please refer to instructions dump. */
        @android.annotation.SuppressLint({"NewApi"})
        public static File getExternalCacheDir(Context r6, boolean r7) {
            /*
            if (r7 != 0) goto L_0x000d;
        L_0x0002:
            r2 = android.os.Build.VERSION.SDK_INT;
            r3 = 8;
            if (r2 < r3) goto L_0x000d;
        L_0x0008:
            r1 = r6.getExternalCacheDir();
        L_0x000c:
            return r1;
        L_0x000d:
            r3 = com.morecruit.ext.utils.StorageUtils.InnerEnvironment.class;
            monitor-enter(r3);
            r2 = new java.lang.StringBuilder;	 Catch:{ all -> 0x0051 }
            r2.<init>();	 Catch:{ all -> 0x0051 }
            r4 = r6.getPackageName();	 Catch:{ all -> 0x0051 }
            r4 = r2.append(r4);	 Catch:{ all -> 0x0051 }
            if (r7 == 0) goto L_0x0054;
        L_0x001f:
            r2 = "-ext";
        L_0x0021:
            r2 = r4.append(r2);	 Catch:{ all -> 0x0051 }
            r2 = r2.toString();	 Catch:{ all -> 0x0051 }
            r1 = getExternalStorageAppCacheDirectory(r2);	 Catch:{ all -> 0x0051 }
            r2 = r1.exists();	 Catch:{ all -> 0x0051 }
            if (r2 != 0) goto L_0x0062;
        L_0x0033:
            r2 = new java.io.File;	 Catch:{ IOException -> 0x0057 }
            r4 = getExternalStorageAndroidDataDir();	 Catch:{ IOException -> 0x0057 }
            r5 = ".nomedia";
            r2.<init>(r4, r5);	 Catch:{ IOException -> 0x0057 }
            r2.createNewFile();	 Catch:{ IOException -> 0x0057 }
        L_0x0041:
            r2 = r1.mkdirs();	 Catch:{ all -> 0x0051 }
            if (r2 != 0) goto L_0x0062;
        L_0x0047:
            r2 = "InnerEnvironment";
            r4 = "Unable to create external cache directory";
            android.util.Log.w(r2, r4);	 Catch:{ all -> 0x0051 }
            r1 = 0;
            monitor-exit(r3);	 Catch:{ all -> 0x0051 }
            goto L_0x000c;
        L_0x0051:
            r2 = move-exception;
            monitor-exit(r3);	 Catch:{ all -> 0x0051 }
            throw r2;
        L_0x0054:
            r2 = "";
            goto L_0x0021;
        L_0x0057:
            r0 = move-exception;
            r2 = "InnerEnvironment";
            r4 = r0.getMessage();	 Catch:{ all -> 0x0051 }
            com.morecruit.ext.component.logger.Logger.i(r2, r4);	 Catch:{ all -> 0x0051 }
            goto L_0x0041;
        L_0x0062:
            monitor-exit(r3);	 Catch:{ all -> 0x0051 }
            goto L_0x000c;
            */
            throw new UnsupportedOperationException("Method not decompiled: com.morecruit.ext.utils.StorageUtils.InnerEnvironment.getExternalCacheDir(android.content.Context, boolean):java.io.File");
        }

        /* JADX WARNING: inconsistent code. */
        /* Code decompiled incorrectly, please refer to instructions dump. */
        @android.annotation.SuppressLint({"NewApi"})
        public static File getExternalFilesDir(Context r7, String r8, boolean r9) {
            /*
            r2 = 0;
            if (r9 != 0) goto L_0x000e;
        L_0x0003:
            r3 = android.os.Build.VERSION.SDK_INT;
            r4 = 8;
            if (r3 < r4) goto L_0x000e;
        L_0x0009:
            r1 = r7.getExternalFilesDir(r8);
        L_0x000d:
            return r1;
        L_0x000e:
            r4 = com.morecruit.ext.utils.StorageUtils.InnerEnvironment.class;
            monitor-enter(r4);
            r3 = new java.lang.StringBuilder;	 Catch:{ all -> 0x0059 }
            r3.<init>();	 Catch:{ all -> 0x0059 }
            r5 = r7.getPackageName();	 Catch:{ all -> 0x0059 }
            r5 = r3.append(r5);	 Catch:{ all -> 0x0059 }
            if (r9 == 0) goto L_0x0052;
        L_0x0020:
            r3 = "-ext";
        L_0x0022:
            r3 = r5.append(r3);	 Catch:{ all -> 0x0059 }
            r3 = r3.toString();	 Catch:{ all -> 0x0059 }
            r1 = getExternalStorageAppFilesDirectory(r3);	 Catch:{ all -> 0x0059 }
            r3 = r1.exists();	 Catch:{ all -> 0x0059 }
            if (r3 != 0) goto L_0x0055;
        L_0x0034:
            r3 = new java.io.File;	 Catch:{ IOException -> 0x008b }
            r5 = getExternalStorageAndroidDataDir();	 Catch:{ IOException -> 0x008b }
            r6 = ".nomedia";
            r3.<init>(r5, r6);	 Catch:{ IOException -> 0x008b }
            r3.createNewFile();	 Catch:{ IOException -> 0x008b }
        L_0x0042:
            r3 = r1.mkdirs();	 Catch:{ all -> 0x0059 }
            if (r3 != 0) goto L_0x0055;
        L_0x0048:
            r3 = "InnerEnvironment";
            r5 = "Unable to create external files directory";
            android.util.Log.w(r3, r5);	 Catch:{ all -> 0x0059 }
            monitor-exit(r4);	 Catch:{ all -> 0x0059 }
            r1 = r2;
            goto L_0x000d;
        L_0x0052:
            r3 = "";
            goto L_0x0022;
        L_0x0055:
            if (r8 != 0) goto L_0x005c;
        L_0x0057:
            monitor-exit(r4);	 Catch:{ all -> 0x0059 }
            goto L_0x000d;
        L_0x0059:
            r2 = move-exception;
            monitor-exit(r4);	 Catch:{ all -> 0x0059 }
            throw r2;
        L_0x005c:
            r0 = new java.io.File;	 Catch:{ all -> 0x0059 }
            r0.<init>(r1, r8);	 Catch:{ all -> 0x0059 }
            r3 = r0.exists();	 Catch:{ all -> 0x0059 }
            if (r3 != 0) goto L_0x0088;
        L_0x0067:
            r3 = r0.mkdirs();	 Catch:{ all -> 0x0059 }
            if (r3 != 0) goto L_0x0088;
        L_0x006d:
            r3 = "InnerEnvironment";
            r5 = new java.lang.StringBuilder;	 Catch:{ all -> 0x0059 }
            r5.<init>();	 Catch:{ all -> 0x0059 }
            r6 = "Unable to create external media directory ";
            r5 = r5.append(r6);	 Catch:{ all -> 0x0059 }
            r5 = r5.append(r0);	 Catch:{ all -> 0x0059 }
            r5 = r5.toString();	 Catch:{ all -> 0x0059 }
            android.util.Log.w(r3, r5);	 Catch:{ all -> 0x0059 }
            monitor-exit(r4);	 Catch:{ all -> 0x0059 }
            r1 = r2;
            goto L_0x000d;
        L_0x0088:
            monitor-exit(r4);	 Catch:{ all -> 0x0059 }
            r1 = r0;
            goto L_0x000d;
        L_0x008b:
            r3 = move-exception;
            goto L_0x0042;
            */
            throw new UnsupportedOperationException("Method not decompiled: com.morecruit.ext.utils.StorageUtils.InnerEnvironment.getExternalFilesDir(android.content.Context, java.lang.String, boolean):java.io.File");
        }
    }

    private StorageUtils() {
    }

    public static boolean isExternalReadable(Context context) {
        return isExternalMounted(context) || isExternalMountedReadOnly(context);
    }

    public static boolean isExternalWritable(Context context) {
        return isExternalMounted(context);
    }

    public static long getExternalCapability() {
        if (!isExternalReadable(null)) {
            return -1;
        }
        StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
        return ((long) stat.getBlockCount()) * ((long) stat.getBlockSize());
    }

    public static long getExternalRemaining() {
        if (!isExternalReadable(null)) {
            return -1;
        }
        StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
        return ((long) stat.getAvailableBlocks()) * ((long) stat.getBlockSize());
    }

    private static boolean isExternalMounted(Context context) {
        if (sMonitoredExternalState == -1) {
            int state = retrieveExternalStorageState();
            if (registerReceiverIfNeeded(context)) {
                sMonitoredExternalState = state;
            }
            if (state != 0) {
                return false;
            }
            return true;
        } else if (sMonitoredExternalState == 0) {
            return true;
        } else {
            return false;
        }
    }

    private static boolean isExternalMountedReadOnly(Context context) {
        if (sMonitoredExternalState == -1) {
            int state = retrieveExternalStorageState();
            if (registerReceiverIfNeeded(context)) {
                sMonitoredExternalState = state;
            }
            if (state != 1) {
                return false;
            }
            return true;
        } else if (sMonitoredExternalState == 1) {
            return true;
        } else {
            return false;
        }
    }

    public static long getInternalCapability() {
        StatFs stat = new StatFs(Environment.getDataDirectory().getPath());
        return ((long) stat.getBlockCount()) * ((long) stat.getBlockSize());
    }

    public static long getInternalRemaining() {
        StatFs stat = new StatFs(Environment.getDataDirectory().getPath());
        return ((long) stat.getAvailableBlocks()) * ((long) stat.getBlockSize());
    }

    static void onStorageStateChanged() {
        sMonitoredExternalState = retrieveExternalStorageState();
    }

    private static boolean registerReceiverIfNeeded(Context context) {
        if (sReceiverRegistered) {
            return true;
        }
        if (context == null || context.getApplicationContext() == null) {
            return false;
        }
        synchronized (sReceiver) {
            if (sReceiverRegistered) {
                return true;
            }
            IntentFilter filter = new IntentFilter();
            filter.addAction("android.intent.action.MEDIA_BAD_REMOVAL");
            filter.addAction("android.intent.action.MEDIA_EJECT");
            filter.addAction("android.intent.action.MEDIA_MOUNTED");
            filter.addAction("android.intent.action.MEDIA_REMOVED");
            filter.addAction("android.intent.action.MEDIA_UNMOUNTED");
            filter.addDataScheme("file");
            context.getApplicationContext().registerReceiver((BroadcastReceiver) sReceiver.get(null), filter);
            sReceiverRegistered = true;
            return true;
        }
    }

    private static int retrieveExternalStorageState() {
        String externalState = Environment.getExternalStorageState();
        if ("mounted".equals(externalState)) {
            return 0;
        }
        if ("mounted_ro".equals(externalState)) {
            return 1;
        }
        return 2;
    }

    private static String getCacheDir(Context context, String name) {
        return getCacheDir(context, name, false);
    }

    public static String getCacheDir(Context context, String name, boolean persist) {
        String dir = getExternalCacheDir(context, name, persist);
        return dir != null ? dir : getInternalCacheDir(context, name, persist);
    }

    public static String getExternalCacheDir(Context context, String name) {
        return getExternalCacheDir(context, name, false);
    }

    public static String getExternalCacheDir(Context context, String name, boolean persist) {
        String dir = getExternalCacheDir(context, persist);
        if (dir == null) {
            return null;
        }
        if (isEmpty(name)) {
            return dir;
        }
        File file = new File(dir + File.separator + name);
        if (!(file.exists() && file.isDirectory())) {
            synchronized (sCacheDirLock) {
                if (!file.isDirectory()) {
                    file.delete();
                    file.mkdirs();
                } else if (!file.exists()) {
                    file.mkdirs();
                }
            }
        }
        return file.getAbsolutePath();
    }

    public static String getExternalCacheDir(Context context, boolean persist) {
        if (!isExternalWritable(context)) {
            return null;
        }
        File externalDir;
        if (persist) {
            externalDir = InnerEnvironment.getExternalFilesDir(context, "cache", false);
        } else {
            externalDir = InnerEnvironment.getExternalCacheDir(context, false);
        }
        if (externalDir != null) {
            return externalDir.getAbsolutePath();
        }
        return null;
    }

    public static String getExternalCacheDirExt(Context context, String name) {
        return getExternalCacheDirExt(context, name, false);
    }

    public static String getExternalCacheDirExt(Context context, String name, boolean persist) {
        String dir = getExternalCacheDirExt(context, persist);
        if (dir == null) {
            return null;
        }
        if (isEmpty(name)) {
            return dir;
        }
        File file = new File(dir + File.separator + name);
        if (!(file.exists() && file.isDirectory())) {
            synchronized (sCacheDirLock) {
                if (!file.isDirectory()) {
                    file.delete();
                    file.mkdirs();
                } else if (!file.exists()) {
                    file.mkdirs();
                }
            }
        }
        return file.getAbsolutePath();
    }

    public static String getExternalCacheDirExt(Context context, boolean persist) {
        if (!isExternalWritable(context)) {
            return null;
        }
        File externalDir;
        if (persist) {
            externalDir = InnerEnvironment.getExternalFilesDir(context, "cache", true);
        } else {
            externalDir = InnerEnvironment.getExternalCacheDir(context, true);
        }
        if (externalDir != null) {
            return externalDir.getAbsolutePath();
        }
        return null;
    }

    public static String getInternalCacheDir(Context context, String name) {
        return getInternalCacheDir(context, name, false);
    }

    public static String getInternalCacheDir(Context context, String name, boolean persist) {
        String dir = getInternalCacheDir(context, persist);
        if (isEmpty(name)) {
            return dir;
        }
        File file = new File(dir + File.separator + name);
        if (!(file.exists() && file.isDirectory())) {
            synchronized (sCacheDirLock) {
                if (!file.isDirectory()) {
                    file.delete();
                    file.mkdirs();
                } else if (!file.exists()) {
                    file.mkdirs();
                }
            }
        }
        return file.getAbsolutePath();
    }

    public static String getInternalCacheDir(Context context, boolean persist) {
        if (persist) {
            return context.getFilesDir().getAbsolutePath() + File.separator + "cache";
        }
        return context.getCacheDir().getAbsolutePath();
    }

    public static boolean isInternal(String path) {
        return path != null && path.startsWith(Environment.getDataDirectory().getAbsolutePath());
    }

    public static boolean isData(String path) {
        return path != null && (path.startsWith(Environment.getDataDirectory().getAbsolutePath()) || path.startsWith(InnerEnvironment.getExternalStorageAndroidDataDir().getAbsolutePath()));
    }

    private static boolean isEmpty(String str) {
        return str == null || str.length() == 0;
    }
}
